package com.example.librarysystem.Controller;

import com.example.librarysystem.service.ViewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/views")
@Tag(name = "视图查询", description = "数据库视图数据查询接口")
public class ViewController {

    private final ViewService viewService;
    public ViewController(ViewService viewService) {
        this.viewService = viewService;
    }
    /**
     * 获取所有视图名称
     */
    @Operation(summary = "获取所有视图名称", description = "返回数据库中所有可用视图的名称列表")
    @GetMapping
    public ResponseEntity<List<String>> getViewNames() {
        List<String> viewNames = viewService.getViewNames();
        return ResponseEntity.ok(viewNames);
    }

    /**
     * 查询指定视图数据
     */
    @Operation(summary = "查询视图数据", description = "根据视图名称查询对应视图的数据")
    @GetMapping("/{viewName}")
    public ResponseEntity<List<Map<String, Object>>> queryView(
            @Parameter(description = "视图名称", required = true)
            @PathVariable String viewName
    ) {

        List<Map<String, Object>> result = viewService.queryView(viewName);

        if (result == null) {

            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }

        return ResponseEntity.ok(result);
    }
}
